"use strict";

exports.__esModule = true;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * 小程序storage
 *
 * @example
 *
 * let storage = new Storage(wx);
 *
 * store.set('userinfo',{
 *   username: 'axetroy',
 *   age: 'Forever Young'
 * });
 *
 * console.log(store.get('userinfo'));
 */
var Storage = exports.Storage = function () {
    function Storage(wx) {
        _classCallCheck(this, Storage);

        this.wx = wx;
    }

    Storage.__isExpired = function __isExpired(entity) {
        if (!entity) return true;
        return Storage.timestamp - (entity.timestamp + entity.expiration) >= 0;
    };

    Storage.prototype.set = function set(key, value, expiration) {
        var entity = {
            timestamp: Storage.timestamp,
            expiration: expiration,
            key: key,
            value: value
        };
        this.wx.setStorageSync(key, entity);
        return this;
    };

    Storage.prototype.get = function get(key) {
        var entity = void 0;
        try {
            entity = this.wx.getStorageSync(key);
            if (entity) {
                // entity = entity;
            } else {
                return null;
            }
        } catch (err) {
            console.error(err);
            return null;
        }

        // 没有设置过期时间, 则直接返回值
        if (!entity.expiration) return entity.value;

        // 已过期
        if (Storage.__isExpired(entity)) {
            this.remove(key);
            return null;
        } else {
            return entity.value;
        }
    };

    Storage.prototype.remove = function remove(key) {
        try {
            this.wx.removeStorageSync(key);
        } catch (err) {
            console.error(err);
        }
        return this;
    };

    Storage.prototype.clear = function clear() {
        try {
            this.wx.clearStorageSync();
        } catch (err) {
            console.error(err);
        }
        return this;
    };

    _createClass(Storage, [{
        key: "info",
        get: function get() {
            var info = {};

            try {
                info = this.wx.getStorageInfoSync();
            } catch (err) {
                console.error(err);
            }

            return info || {};
        }
    }, {
        key: "length",
        get: function get() {
            return (this.info.keys || []).length;
        }
    }], [{
        key: "timestamp",
        get: function get() {
            return new Date().valueOf();
        }
    }, {
        key: "__info",
        get: function get() {
            var info = {};
            try {
                info = this.wx.getStorageInfoSync() || info;
            } catch (err) {
                console.error(err);
            }
            return info;
        }
    }]);

    return Storage;
}();

var MLocalStorage = exports.MLocalStorage = function () {
    function MLocalStorage() {
        _classCallCheck(this, MLocalStorage);
    }

    MLocalStorage.setItem = function setItem(k, v) {
        MLocalStorage.store.set(k, v);
    };

    MLocalStorage.getItem = function getItem(k) {
        return MLocalStorage.store.get(k);
    };

    return MLocalStorage;
}();

Object.defineProperty(MLocalStorage, "store", {
    enumerable: true,
    writable: true,
    value: new Storage(wx)
});